package com.lukamikec.kmap;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.*;
import android.widget.*;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity {

	Button 				konj, dis, nego, kreni;
	ArrayList<Button>	slovca_btns;
	EditText 			form_tbox;
	
	public void dialog (String str)
	{
		AlertDialog alertDialog;
		alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle("K-MAP");
		alertDialog.setMessage(str);
		alertDialog.show();
	}
	
	void insert_at_cursor(String str)
	{
		int bef = form_tbox.getSelectionStart();
		form_tbox.setText(
				form_tbox.getText().insert(bef, str));
		form_tbox.setSelection(bef + str.length());
	}
	
	void kmap_go()
	{
		String formula_txt = form_tbox.getText() + "";
		Intent myIntent = new Intent(getBaseContext(), Akt_kmap.class);
		myIntent.putExtra("frml", formula_txt);
	
	    startActivityForResult(myIntent, 0);
    

	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        konj = 		(Button)findViewById(R.id.Button01);
        dis  = 		(Button)findViewById(R.id.Button02);
        nego = 		(Button)findViewById(R.id.button1);
        kreni=      (Button)findViewById(R.id.button2);
        form_tbox = (EditText)findViewById(R.id.editText1);
        
        
       
        slovca_btns = new ArrayList<Button>();
        slovca_btns.add((Button) findViewById(R.id.Button03));
        slovca_btns.add((Button) findViewById(R.id.Button04));
        slovca_btns.add((Button) findViewById(R.id.Button05));
        slovca_btns.add((Button) findViewById(R.id.Button06));
        slovca_btns.add((Button) findViewById(R.id.Button07));
        slovca_btns.add((Button) findViewById(R.id.Button08));
        slovca_btns.add((Button) findViewById(R.id.Button09));
        slovca_btns.add((Button) findViewById(R.id.Button10));
        
        
        
        konj.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	insert_at_cursor(" * ");
            }
          });
        
        dis.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	insert_at_cursor(" + ");
            }
          });
        
        nego.setOnClickListener(new OnClickListener() {
            @Override public void onClick(View v) {
            	insert_at_cursor("!");
            }
          });
        
        for (int k = 0; k < slovca_btns.size(); ++k)
        {
        	slovca_btns.get(k).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                	insert_at_cursor((String) ((Button)v).getText());
                	//insert_at_cursor("s");
                }
              });

        }
        
        kreni.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            	kmap_go();
            }
          });
        
         form_tbox.setOnClickListener(new OnClickListener() {
             @Override public void onClick(View v) {
            	 //getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
             }
           });
         
         form_tbox.setOnEditorActionListener(new OnEditorActionListener() {
        	    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        	    //    if (actionId == EditorInfo.IME_ACTION_DONE) {
        	            kmap_go();
        	      //      return true;
        	       // }
        	        //return false;
        	            return false;
        	    }
           });

         //form_tbox.setText("(A + B + C + D) * E * F + G");
         //kmap_go();
         
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch (item.getItemId()) {
    		case R.id.menu_settings:
				 dialog("Karnaugh diagram generator.\nAuthor: Luka Mikec\n2012");
				 break;
    	}
    	return true;
	}
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
